package com.ddm.controller;

import com.ddm.service.IEmployeeService;
import com.dto.AjaxResult;
import com.entity.Employee;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by he on 2025/3/4 13:25
 */
@RequestMapping("/admin/employee")
@RestController
public class EmployeeController {

    @Autowired
    public IEmployeeService employeeService;

    // 根据员工ID查询
    @GetMapping("/{employeeId}")
    public AjaxResult getEmployeeById(@PathVariable("employeeId") Integer employeeId) {
        Employee employee = employeeService.selectEmployeeById(employeeId);
        return employee != null ? AjaxResult.success(employee) : AjaxResult.error("员工未找到");
    }

    // 新增员工
    @PostMapping
    public AjaxResult addEmployee(@RequestBody Employee employee) {
        int rows = employeeService.insertEmployee(employee);
        return rows > 0 ? AjaxResult.success("新增员工成功") : AjaxResult.error("新增员工失败");
    }

    // 修改员工信息
    @PutMapping
    public AjaxResult editEmployee(@RequestBody Employee employee) {
        int rows = employeeService.updateEmployee(employee);
        return rows > 0 ? AjaxResult.success("修改员工信息成功") : AjaxResult.error("修改员工信息失败");
    }

    // 根据员工ID删除
    @DeleteMapping("/{employeeId}")
    public AjaxResult deleteEmployeeById(@PathVariable Integer employeeId) {
        int rows = employeeService.deleteEmployeeById(employeeId);
        return rows > 0 ? AjaxResult.success("删除员工成功") : AjaxResult.error("删除员工失败");
    }

    // 批量删除员工
    @DeleteMapping("/batch/{employeeIds}")
    public AjaxResult deleteEmployeeBatch(@PathVariable Integer[] employeeIds) {
        int rows = employeeService.deleteEmployeeByIds(employeeIds);
        return rows > 0 ? AjaxResult.success("批量删除员工成功") : AjaxResult.error("批量删除员工失败");
    }

    // 分页查询员工
    @GetMapping("/page")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,@RequestParam(required = false)  Employee employee) {
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> list = employeeService.selectEmployeeList(employee);
        PageInfo<List> pageInfo = new PageInfo(list);
        return AjaxResult.success(pageInfo);
    }
}

